local CheckWidgetType = UI.CheckWidgetType
local CheckArgType = UI.CheckArgType
local CheckArgWidget = UI.CheckArgWidget
local MakeAccessor = UI.MakeAccessor
local MakeTypeSetter = UI.MakeTypeSetter

--[[ EditBox @FIXME ]]--
local EditBox = UI.NewWidgetType("EditBox", {"Frame", "FontInstance"})
EditBox.parameters = {
	text = nil,
	maxbytes = 0,
	maxletters = 0,
	autofocus = false,
	numeric = false,
	insets_left = 0,
	insets_right = 0,
	insets_top = 0,
	insets_bottom = 0,
	text_red = 1,
	text_green = 1,
	text_blue = 1,
	history = {},
	history_size = 10,
}

function EditBox:Initialize(o)
	local t = UI.params[o]
	assert(t)
end

MakeTypeSetter("EditBox", "SetMaxBytes", "maxbytes", "number")
MakeAccessor("EditBox", "GetMaxBytes", "maxbytes")
MakeTypeSetter("EditBox", "SetMaxLetters", "maxletters", "number")
MakeAccessor("EditBox", "GetMaxLetters", "maxletters")

MakeTypeSetter("EditBox", "SetTextInsets", {"insets_left", "insets_right", "insets_top", "insets_bottom"}, {"number", "number", "number", "number"})
MakeAccessor("EditBox", "GetTextInsets", {"insets_left", "insets_right", "insets_top", "insets_bottom"})

MakeTypeSetter("EditBox", "SetTextColor", {"text_red", "text_green", "text_blue"}, {"number", "number", "number"})
MakeAccessor("EditBox", "GetTextColor", {"text_red", "text_green", "text_blue"})

MakeAccessor("EditBox", "GetHistoryLines", "history_size")

function EditBox.prototype:SetHistoryLines(value)
	CheckWidgetType("EditBox:SetHistoryLines", self, "EditBox")
	CheckArgType("EditBox:SetHistoryLines", 2, "lines", function (v) return type(v) == "number" and v >= 1 end, value)

	local t = UI.params[self]
	t.history_size = value
	for i = #t.history, value, -1 do
		t.history[i] = nil
	end
end

function EditBox.prototype:AddHistoryLine(text)
	CheckWidgetType("EditBox:AddHistoryLine", self, "EditBox")
	CheckArgType("EditBox:AddHistoryLine", 2, "text", "string", text)
	table.insert(UI.params[self].history, 1, text)
end

function EditBox.prototype:SetAutoFocus(state)
	CheckWidgetType("EditBox:SetAutoFocus", self, "EditBox")
	UI.params[self].autofocus = state and true or false
end

function EditBox.prototype:SetNumeric(state)
	CheckWidgetType("EditBox:SetNumeric", self, "EditBox")
	UI.params[self].numeric = state and true or false
end

MakeAccessor("EditBox", "IsNumeric", "numeric")

function EditBox.prototype:SetText(value)
	CheckWidgetType("EditBox:SetText", self, "EditBox")
	local t = UI.params[self]
	assert(not t.numeric)
	t.text = tostring(value)
	UI.CallScript(self, "OnTextSet")
	UI.CallScript(self, "OnCursorChanged", 0, 0, 2, t.height)
end

function EditBox.prototype:SetNumber(value)
	CheckWidgetType("EditBox:SetNumber", self, "EditBox")
	local t = UI.params[self]
	assert(t.numeric)
	t.text = tonumber(value)
	UI.CallScript(self, "OnTextSet")
	UI.CallScript(self, "OnCursorChanged", 0, 0, 2, t.height)
end

function EditBox.prototype:GetText()
	CheckWidgetType("EditBox:GetText", self, "EditBox")
	local t = UI.params[self]
	assert(not t.numeric)
	return tostring(t.text)
end

function EditBox.prototype:GetNumber()
	CheckWidgetType("EditBox:GetNumber", self, "EditBox")
	local t = UI.params[self]
	assert(t.numeric)
	return tonumber(t.text)
end

function EditBox.prototype:ClearFocus()
	CheckWidgetType("EditBox:ClearFocus", self, "EditBox")
end

function EditBox.prototype:SetCursorPosition(p)
	CheckWidgetType("EditBox:SetCursorPosition", self, "EditBox")
	UI.CallScript(self, "OnCursorChanged", 0, 0, 2, t.height)
end

function EditBox.prototype:HighlightText(start)
	CheckWidgetType("EditBox:HighlightText", self, "EditBox")
end

UI.RegisterScriptHandlers(EditBox,
	"OnSpacePressed", "OnEnterPressed", "OnTabPressed", "OnEscapePressed",
	"OnEditFocusGained", "OnEditFocusLost",
	"OnCharComposition",
	"OnCursorChanged", "OnInputLanguageChanged", "OnTextChanged", "OnTextSet")
